package com.vote.web.controller.business;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.vote.common.annotation.Log;
import com.vote.common.core.controller.BaseController;
import com.vote.common.core.domain.AjaxResult;
import com.vote.common.enums.BusinessType;
import com.vote.business.domain.VoteUser;
import com.vote.business.service.IVoteUserService;
import com.vote.common.utils.poi.ExcelUtil;
import com.vote.common.core.page.TableDataInfo;

/**
 * 投票用户Controller
 * 
 * @author ruoyi
 * @date 2025-07-16
 */
@RestController
@RequestMapping("/business/voteUser")
public class VoteUserController extends BaseController
{
    @Autowired
    private IVoteUserService voteUserService;

    /**
     * 查询投票用户列表
     */
    @PreAuthorize("@ss.hasPermi('business:voteUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(VoteUser voteUser)
    {
        startPage();
        List<VoteUser> list = voteUserService.selectVoteUserList(voteUser);
        return getDataTable(list);
    }

    /**
     * 导出投票用户列表
     */
    @PreAuthorize("@ss.hasPermi('business:voteUser:export')")
    @Log(title = "投票用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VoteUser voteUser)
    {
        List<VoteUser> list = voteUserService.selectVoteUserList(voteUser);
        ExcelUtil<VoteUser> util = new ExcelUtil<VoteUser>(VoteUser.class);
        util.exportExcel(response, list, "投票用户数据");
    }

    /**
     * 获取投票用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:voteUser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(voteUserService.selectVoteUserById(id));
    }

    /**
     * 新增投票用户
     */
    @PreAuthorize("@ss.hasPermi('business:voteUser:add')")
    @Log(title = "投票用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VoteUser voteUser)
    {

        boolean isExist=voteUserService.checkVoteUserIsExist(voteUser.getPhone());
        if(voteUserService.checkVoteUserIsExist(voteUser.getPhone())){
            return AjaxResult.error("用户已经存在");
        }
        return toAjax(voteUserService.insertVoteUser(voteUser));
    }

    /**
     * 修改投票用户
     */
    @PreAuthorize("@ss.hasPermi('business:voteUser:edit')")
    @Log(title = "投票用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VoteUser voteUser)
    {
        return toAjax(voteUserService.updateVoteUser(voteUser));
    }

    /**
     * 删除投票用户
     */
    @PreAuthorize("@ss.hasPermi('business:voteUser:remove')")
    @Log(title = "投票用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(voteUserService.deleteVoteUserByIds(ids));
    }
}
